Type conversion and Casting
➢ It is fairly common to assign a value of one type to a variable of another
type. If the two types are compatible, then Java will perform the
conversion automatically.
➢ When one type of data is assigned to another type of variable, an
automatic type conversion will take place if the following two conditions
are met:
• The two types are compatible.
• The destination type is larger than the source type.
➢ When these two conditions are met, a widening conversion takes place.
For example, the int type is always large enough to hold all valid byte
values, so no explicit cast statement is required.
➢ if we try to assign int typed value(larger ranged value) to byte(smaller
ranged value), error would arise.[As here we are doing wrong assignment,
so java compiler will not perform narrowing automatically]
➢ If we want to perform narrowing, we need to use explicit type casting